import sys
import locale
import pytz

from http.server import HTTPServer
from apscheduler.schedulers.background import BackgroundScheduler

from config import config
from bot_handler import get_bot_handler
from daliy_task import fetchAndClass, republish_recommend

locale.setlocale(locale.LC_CTYPE, 'zh_CN')


# python /home/cchang/arXivPost/paper_bot.py 8200
def run(port=8200):
    RequestHandler = get_bot_handler(config)
    httpd = HTTPServer(('', port), RequestHandler)
    print("start ......")
    httpd.serve_forever()

def run_scheduler():
    scheduler = BackgroundScheduler()
    scheduler.add_job(fetchAndClass, 'cron', day_of_week='mon-fri', hour='13', minute='5', timezone=pytz.timezone('Asia/Shanghai'))
    scheduler.add_job(republish_recommend, 'cron', day_of_week='sat', hour='13', minute='5', timezone=pytz.timezone('Asia/Shanghai'))
    scheduler.start()


if __name__ == '__main__':
    run_scheduler()
    try:
        run(int(sys.argv[1]))
    except ValueError:
        run()
